package com.yjf.ljr.activity;

import java.net.URLEncoder;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.UpdatePwdClass;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AESUtil;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.util.Base64;

public class UpdatePwdActivity extends IjActivity implements OnClickListener {
	private Button mBtUpdatePwd;// 确认
	private TextView mEtUpdatePwdold;// 旧密码
	private TextView mEtUpdatePwdnew;// 新密码
	private TextView mEtUpdatePwdnewto;// 确认新密码
	private String password;
	private String newPassword;
	private String newPasswordTo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acticity_updatepwd);
		initControl();
	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("修改登录密码");
		mBtUpdatePwd = (Button) findViewById(R.id.btupdatepwd);
		mEtUpdatePwdold = (EditText) findViewById(R.id.etoldpwd);
		mEtUpdatePwdnew = (EditText) findViewById(R.id.etnewpwd);
		mEtUpdatePwdnewto = (EditText) findViewById(R.id.etnewpwdagain);
		mBtUpdatePwd.setOnClickListener(this);

	}

	class UpdatePwdTask extends AsyncTask<Void, Void, Void> {
		private String password;// 旧密码
		private String newPassword;// 新密码
		private String newPasswordTo;// newPasswordTo
		private String type;// 密码类型
		private String mFaillog;
		private UpdatePwdClass date = new UpdatePwdClass();

		public UpdatePwdTask(String password, String newPassword, String newPasswordTo) {
			this.password = password;
			this.newPassword = newPassword;
			this.newPasswordTo = newPasswordTo;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			RequestObject requestObject = new RequestObject();
			requestObject.method = "appChangePwd";
			String pwdStr = URLEncoder.encode(password);
			String newPwdStr = URLEncoder.encode(newPassword);
			requestObject.map.put("password", pwdStr);
			requestObject.map.put("newPassword", newPwdStr);

			String RequestUrl = RequestBuilder.build(requestObject);
			try {

				IjConnect.doRequest(RequestUrl, date);
				if (date.code == null) {
					mFaillog = getResources().getString(R.string.loaddata_exception);
				} else if (!date.code.equals(CommonData.RESULT_SUCCESS) && !("1".equals(date.isSuccess))) {
					mFaillog = date.msg;
				}

			} catch (Exception e) {
				// TODO: handle exception
				mFaillog = getResources().getString(R.string.loaddata_exception);
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (!TextUtils.isEmpty(mFaillog)) {

				if (!newPassword.equals(newPasswordTo)) {
					showToast("两次输入的新密码不一致");
					dismissProgressDialog();
				} else {
					showToast(mFaillog);
					dismissProgressDialog();
				}

			} else {
				if (date.code != null && date.code.equals(CommonData.RESULT_SUCCESS)) {
					showToast(date.msg);
					dismissProgressDialog();
					setResult(RESULT_OK);
					UpdatePwdActivity.this.finish();
				}
			}
		}

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btupdatepwd:

			password = mEtUpdatePwdold.getText().toString().trim();
			newPassword = mEtUpdatePwdnew.getText().toString().trim();
			newPasswordTo = mEtUpdatePwdnewto.getText().toString().trim();
			if (TextUtils.isEmpty(password)) {
				showToast("请输入旧密码");
				return;
			}
			if (password.length() < 6 || password.length() > 20) {
				showToast("密码长度应该在6-20个字符范围内");
				return;
			}
			if (TextUtils.isEmpty(newPassword)) {
				showToast("请输入新密码");
				return;
			}
			if (isChinese(newPassword)) {
				showToast("新密码不能包含中文");
				return;
			}
			if (newPassword.length() < 6 || newPassword.length() > 20) {
				showToast("新设置的密码长度应该在6-20个字符范围内");
				return;
			}
			if (AppUtil.isNumeric(newPassword)) {
				showToast("新密码不能全为数字");
				return;
			}
			if (AppUtil.isCharAllSame(newPassword)) {
				showToast("新密码不能使用完全相同的数字、字母或符号");
				return;
			}
			if (!AppUtil.hasDigit(newPassword)) {
				showToast("新密码不能全为字母");
				return;
			}
			if (TextUtils.isEmpty(newPasswordTo)) {
				showToast("请输入重复密码");
				return;
			}
			if (!newPassword.equals(newPasswordTo)) {
				showToast("两次密码不一致");
				return;
			}
			if (password.equals(newPassword)) {
				showToast("新密码不能与旧密码相同");
				return;
			}

			new UpdatePwdTask(AESUtil.encode(password), AESUtil.encode(newPassword),
					AESUtil.encode(newPasswordTo)).execute();
			showProgressDialog("正在修改请稍等！");
			break;

		default:
			break;
		}
	}

	// 判断是不是中文
	public boolean isChinese(String name) {
		String str = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";

		boolean isIncludeChinese = false;
		for (int i = 0; i < name.length(); i++) {
			if (Pattern.matches(str, name.substring(i, i + 1))) {
				isIncludeChinese = true;
				break;
			}
		}
		return isIncludeChinese;
	}

}
